﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace OnlineMealOrdering.Common
{
    public class PagingHelper<T>
    {

        /// <summary>
        /// 当前页
        /// </summary>
        public int PageIndex { get; set; }

        /// <summary>
        /// 单页记录数
        /// </summary>
        public int PageSize { get; set; }

        /// <summary>
        /// 总记录数
        /// </summary>
        public int TotalRecordsNumber { get; set; }

        /// <summary>
        /// 总页数
        /// </summary>
        public int TotalPageNumber { get; set; }

        /// <summary>
        /// 分页后的数据
        /// </summary>
        public List<T> PageData;


        /// <summary>
        /// 生产分页数据
        /// </summary>
        /// <typeparam name="U">Entity 类型</typeparam>
        /// <param name="selector">数据源查询表达式</param>
        /// <param name="converter">转换表达式</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">需返回的记录数</param>
        public void GenenratePageData<U>(IQueryable<U> selector, Func<U, T> converter, int pageIndex, int pageSize)
        {

            PageIndex = pageIndex;
            PageSize = pageSize;
            TotalRecordsNumber = selector.Count();

            if (PageData == null)
                PageData = new List<T>();
            if (PageSize == 0)
            {
                selector.ToList().ForEach(a => PageData.Add(converter(a)));
            }
            else
            {
                selector.Skip((PageIndex - 1) * PageSize).Take(PageSize).ToList().ForEach(a => PageData.Add(converter(a)));
            }

        }

        public void GenenratePageData<U>(IQueryable<U> selector, int pageIndex, int pageSize)
        {

            PageIndex = pageIndex;
            PageSize = pageSize;
            TotalRecordsNumber = selector.Count();

            if (PageData == null)
                PageData = new List<T>();
            if (PageSize == 0)
            {
                selector.ToList();
            }
            else
            {
                selector.Skip((PageIndex - 1) * PageSize).Take(PageSize).ToList();
            }

        }


    }

    public class SortHelper
    {

        public enum EnumSortDirection
        {
            Ascending,
            Descending
        }

        /// <summary>
        /// returns a Func that takes an IQueryable and a bool, and sorts the IQueryable (ascending or descending based on the bool).
        /// The sort is performed on the property identified by the key selector.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="keySelector"></param>
        /// <returns></returns>
        public static Func<IQueryable<T>, bool, IOrderedQueryable<T>> CreateOrderingFunc<T, TKey>(Expression<Func<T, TKey>> keySelector)
        {
            return
                (source, ascending) =>
                ascending
                    ? source.OrderBy(keySelector)
                    : source.OrderByDescending(keySelector);
        }
        /// <summary>
        /// return Descending or Ascending 
        /// </summary>
        /// <param name="sortDirection"></param>
        /// <returns></returns>
        public static EnumSortDirection GetSortDirection(string sortDirection)
        {
            if (sortDirection != null)
            {
                if (sortDirection.Equals("DESC", StringComparison.OrdinalIgnoreCase) ||
                    sortDirection.Equals("DESCENDING", StringComparison.OrdinalIgnoreCase))
                {
                    return EnumSortDirection.Descending;
                }
            }
            return EnumSortDirection.Ascending;
        }



    }
}
